//
//  NoteBookManager.m
//  Grammar
//
//  Created by Rick on 6/30/13.
//
//

#import "NoteBookManager.h"
#import "Note.h"

#define TYNoteBook @"TYNoteBook"
#define TYArchiver  @"TYReadHistoryArchiver"

@interface NoteBookManager()
- (void)restore;
@end

@implementation NoteBookManager
static NoteBookManager *_sharedInst = nil;

+(NoteBookManager*)sharedManager
{
    @synchronized(self){
        if (_sharedInst == nil) {
            _sharedInst = [[self alloc] init];
        }
    }
    return _sharedInst;
}

- (id)init{
    if ((self = [super init])) {
        [self restore];
    }
    return self;
}

- (void)saveData{
    if (_notes) {
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                                     initForWritingWithMutableData:data];
        [archiver encodeObject:_notes forKey:TYArchiver];
        [archiver finishEncoding];
        [USER_DEFAULT setObject:data forKey:[NSString stringWithFormat:@"%@%@",TYNoteBook,_gramarId] ];
        [archiver release];
        [data release];
        
        [USER_DEFAULT synchronize];
    }
}

- (void)readData{
    NSData *data = [USER_DEFAULT objectForKey:[NSString stringWithFormat:@"%@%@",TYNoteBook,_gramarId]];;
    if (!data) {
        return;
    }
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSArray *array = [unarchiver decodeObjectForKey:TYArchiver];
    [unarchiver finishDecoding];
    [_notes addObjectsFromArray:array];
    [unarchiver release];
}

- (void)addNote:(Note *)note
{
    for (Note *aNote in self.notes) {
        if ([aNote.noteCreateTimeStamp isEqualToString:note.noteCreateTimeStamp]) {
            return;
        }
    }
    [_notes insertObject:note atIndex:0];

    [self saveData];
}

- (void)removeNote:(Note *)note
{
    NSArray *array = [NSArray arrayWithArray:self.notes];
    for (Note *aNote in array) {
        if ([aNote.noteCreateTimeStamp isEqualToString:note.noteCreateTimeStamp]) {
            [_notes removeObject:aNote];
            [self saveData];
        }
    }
}

- (void)removeNoteByGrammarId:(NSString*)grammarId
{
    NSArray *array = [NSArray arrayWithArray:self.notes];
    for (Note *aNote in array) {
        if ([aNote.grammarId isEqualToString:grammarId]) {
            [_notes removeObject:aNote];
            [self saveData];
        }
    }
}

- (void)updateNote:(Note*)note
{
    NSArray *array = [NSArray arrayWithArray:self.notes];
    for (Note *aNote in array) {
        if ([aNote.noteCreateTimeStamp isEqualToString:note.noteCreateTimeStamp]) {
            [_notes removeObject:aNote];
        }
    }
    [_notes insertObject:note atIndex:0];
    
    [self saveData];
}

- (NSMutableArray*)notes
{
    [self clearMemoryData];
    [self readData];
    return _notes;
}

- (void)restore{
    self.notes = [NSMutableArray array];
    [self readData];
}

- (void)clearMemoryData
{
    [_notes removeAllObjects];
}

- (void)removeAllNotes
{
    [_notes removeAllObjects];
    [USER_DEFAULT removeObjectForKey:[NSString stringWithFormat:@"%@%@",TYNoteBook,_gramarId]];
    [USER_DEFAULT synchronize];
}

@end
